<!DOCTYPE html>

<p>
On Mac when word-selecting backwards starting with the
caret on the middle of a word and then word-selecting forward, the
caret is left in the same place where it was, instead of directly selecting to the end
of the word (which is windows/unix behavior).
</p>

<div id="test-div" contenteditable=true>
    line 1<br>
    line 2<br>
    line 3
</div>

<script>
function editingTest(behavior) {
    if (window.testRunner && window.internals) {
        testRunner.dumpAsText();
        internals.settings.setEditingBehavior(behavior);
    }

    function getSetCaretFunction(node, container, offset) {
        return function () {
            var selection = window.getSelection();
            selection.empty();

            var range = document.createRange();
            range.setStart(container, offset);
            selection.addRange(range);
        }
    }

    function runTest(firstDirection, secondDirection, granularity, expectedText, setCaret) {
        var selection = window.getSelection();
        setCaret();
        selection.modify("extend", firstDirection, granularity);
        selection.modify("extend", secondDirection, granularity);
        var s = selection.toString();
        document.write("Extend " + firstDirection + " and then " + secondDirection + " by " + granularity + ": ");
        document.write(s === expectedText ? "PASS" : 'FAIL: expected "' + escape(expectedText) + '", got "' + escape(s) + '"');
        document.write("<br>");
    }

    var node = document.getElementById("test-div");
    var children = node.childNodes;

    var wordCaretFunction = getSetCaretFunction(node, children[2], children[2].data.search("ne 2"));

    document.write(behavior + ":<br>");
    runTest("backward", "forward", "word", behavior == "mac" ? "" : behavior == "win" ? "ne " : "ne", getSetCaretFunction(node, children[2], children[2].data.search("ne 2")));
    runTest("forward", "backward", "word", behavior == "mac" ? "" : "li", getSetCaretFunction(node, children[2], children[2].data.search("ne 2")));
    runTest("backward", "forward", "line", behavior == "mac" ? "" : "1\nline ", getSetCaretFunction(node, children[0], children[0].data.search("1")));
    runTest("forward", "backward", "line", behavior == "mac" ? "" : "2\nline ", getSetCaretFunction(node, children[4], children[4].data.search("3")));
    runTest("backward", "forward", "paragraph", behavior == "mac" ? "" : "1\nline ", getSetCaretFunction(node, children[0], children[0].data.search("1")));
    runTest("forward", "backward", "paragraph", behavior == "mac" ? "" : "2\nline ", getSetCaretFunction(node, children[4], children[4].data.search("3")));
}

editingTest("mac");
editingTest("win");
editingTest("unix");
editingTest("android");

var node = document.getElementById("test-div");
node.parentNode.removeChild(node);
</script>
